﻿using System;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;
using System.Xml.Linq;
using Ice.Extension;
using SwitchIPModifier.Core;

namespace SwitchIPModifier
{
    public partial class ConfigForm : Form
    {
        private Config _config;
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            var names = SerialPort.GetPortNames();
            comboBox1.Items.AddRange(names);

            _config = IniHelper.GetConfig();
            txtTitle.Text = _config.Title;
            comboBox1.Text = _config.SeriapPortName;
            txtLogDir.Text = _config.LogDir;





            txtBaudRate.DataBindings.Add("Text", _config, "BaudRate");
            txtAccount.DataBindings.Add("Text", _config, "Account");
            txtPassword.DataBindings.Add("Text", _config, "Password");


            txtIp.Text = _config.Ip;
            txtSnLength.Text = _config.SnLength.ToString();

            checkBox1.Checked = _config.IsScanSn;
        }


        private void Button1_Click(object sender, EventArgs e)
        {
            _config.Title = txtTitle.Text;
            _config.SeriapPortName = comboBox1.Text;
            _config.LogDir = txtLogDir.Text;

            _config.BaudRate = txtBaudRate.Text.ToInt32();
            _config.Account = txtAccount.Text;
            _config.Password = txtPassword.Text;

            _config.Ip = txtIp.Text;
            _config.SnLength = txtSnLength.Text.ToInt32();

            _config.IsScanSn = checkBox1.Checked;

            IniHelper.SaveConfig(_config);
            DialogResult = DialogResult.OK;
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtLogDir.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
